package com.padakeji.android.pworks.module.nav.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.media.Image;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.padakeji.android.pworks.R;
import com.padakeji.android.pworks.module.nav.bean.NavItem;

import org.w3c.dom.Text;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by AtanL on 2015/9/23.
 */
public class NavPagerAdapter extends FragmentPagerAdapter {

    public static class NavTabViewHolder {
        public ImageView iconImage;
        public TextView titleText;
    }

    private Context mContext;
    private LayoutInflater mInflater;
    private List<NavItem> navItems;
    private Map<Integer, View> navTabViews;

    public NavPagerAdapter(@NonNull FragmentManager fm, @NonNull Context context, @NonNull NavItem[] items) {
        super(fm);
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        navItems = Arrays.asList(items);
        navTabViews = new HashMap<>();
    }

    @Override
    public Fragment getItem(int position) {
        return navItems.get(position).fragment;
    }

    @Override
    public int getCount() {
        return navItems.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return navItems.get(position).title;
    }

    public View getPageTabView(int position) {
        View v = navTabViews.get(position);
        if (v == null) {
            v = mInflater.inflate(R.layout.pworks_activity_nav_item_tab, null, false);
            NavTabViewHolder vh = new NavTabViewHolder();
            vh.iconImage = (ImageView) v.findViewById(R.id.pworks_activity_nav_image_tab_icon);
            vh.titleText = (TextView) v.findViewById(R.id.pworks_activity_nav_text_tab_title);
            vh.iconImage.setBackgroundDrawable(getDrawable(navItems.get(position).iconSelected, navItems.get(position).iconUnselected));
            vh.titleText.setText(navItems.get(position).title);
            vh.titleText.setTextColor(navItems.get(position).unselectedTitleColor);
            v.setTag(vh);
            navTabViews.put(position, v);
        }
        return v;
    }

    public NavTabViewHolder getPageTabViewHolder(int position) {
        View v = getPageTabView(position);
        return (NavTabViewHolder) v.getTag();
    }

    public void setPageTabSelected(int position, boolean selected) {
        NavTabViewHolder vh = getPageTabViewHolder(position);
        vh.titleText.setTextColor(selected ? navItems.get(position).selectedTitleColor : navItems.get(position).unselectedTitleColor);
        vh.iconImage.setSelected(selected);
    }

    private Drawable getDrawable(Drawable selected, Drawable unselected) {
        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(new int[] {android.R.attr.state_selected}, selected);
        stateListDrawable.addState(new int[] {}, unselected);
        return stateListDrawable;
    }

}
